package com.cskaoyan.mall.controller.mall;

import com.cskaoyan.mall.model.goods.Type;
import com.cskaoyan.mall.service.*;
import com.cskaoyan.mall.util.Result;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: jyf2000jyf
 */
@WebServlet("/api/mall/index/*")
public class IndexServlet extends HttpServlet {
    private GoodsService goodsService = new GoodsServiceImpl();
    private Gson gson = new Gson();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replace(request.getContextPath() + "/api/mall/index/", "");
        if ("getType".equals(op)) {
            getType(request, response);
        }
    }

    private void getType(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Type> typeList = goodsService.getType();
        Result result = new Result(0, null, typeList);
        response.getWriter().println(gson.toJson(result));
    }
}
